package com.hetrone.machine.graph;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;

/**
 * 稠密图
 * 
 * @author liuwei
 *
 */
public class DenseGraph extends Graph<Boolean> {
	public DenseGraph(Integer n, Boolean directed) {
		super(n, directed);
	}

	@Override
	public List<List<Boolean>> init() {
		List<List<Boolean>> graph = new ArrayList<List<Boolean>>(getN());
		for (int i = 0; i < getN(); i++) {
			List<Boolean> gc = new ArrayList<Boolean>(getN());
			for (int j = 0; j < getN(); j++) {
				gc.add(false);
			}
			graph.add(gc);
		}
		return graph;
	}

	@Override
	public void addEdge(Integer v, Integer w) {
		if (hasEdge(v, w)) {
			return;
		}
		getG().get(v).set(w, true);
		if (!isDirected()) {
			getG().get(w).set(v, true);
		}
		increaseM();
	}

	@Override
	public Boolean hasEdge(Integer v, Integer w) {
		if (v < 0 || v >= getN()) {
			throw new InvalidParameterException("参数v[" + v + "]不合法");
		}
		if (w < 0 || w >= getN()) {
			throw new InvalidParameterException("参数v[" + w + "]不合法");
		}
		return getG().get(v).get(w);
	}
}
